<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>景点管理</title>
    <!--引入的vue.js-->
    <script src="/js/vue.js"></script>
    <!--引入elementUI的css-->
    <link  type="text/css" rel="stylesheet" href="/css/index.css">
    <!--引入elementUI的js-->
    <script src="/js/index.js"></script>
    <!--引入axios.js-->
    <script src="/js/axios.min.js"></script>

    <style>
        .search-container {
            margin-bottom: 20px;
            padding: 15px;
            background-color: #f5f7fa;
            border-radius: 4px;
        }
        .form-section {
            border-bottom: 1px solid #ebeef5;
            margin-bottom: 20px;
            padding-bottom: 15px;
        }
        .form-section-title {
            font-weight: bold;
            color: #409EFF;
            margin-bottom: 15px;
            font-size: 16px;
        }
        .table-container {
            margin-top: 20px;
        }
        .action-buttons {
            display: flex;
            gap: 5px;
        }
        .pagination-container {
            margin-top: 20px;
            text-align: right;
        }
        .dialog-footer {
            text-align: right;
        }
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 120px;
            height: 120px;
            line-height: 120px;
            text-align: center;
        }
        .avatar {
            width: 120px;
            height: 120px;
            display: block;
        }
        .dot-image-cell {
            text-align: center;
        }
        .dot-image {
            width: 60px;
            height: 60px;
            object-fit: cover;
            border-radius: 4px;
        }
        .status-pending {
            color: #909399;
            font-weight: bold;
        }
        .status-passed {
            color: #67C23A;
            font-weight: bold;
        }
        .status-rejected {
            color: #F56C6C;
            font-weight: bold;
        }
    </style>
</head>
<body>
<div id="app">
    <!-- 搜索框-->
    <div class="search-container">
        <el-row :gutter="10">
            <el-col :span="4">
                <el-input v-model="searchForm.dotName" placeholder="请输入景点名称" clearable></el-input>
            </el-col>
            <el-col :span="4">
                <el-input v-model="searchForm.dotAddress" placeholder="请输入景点地址" clearable></el-input>
            </el-col>
            <el-col :span="4">
                <el-select
                        v-model="searchForm.auditStatus"
                        placeholder="请选择审核状态"
                        style="width: 100%;">
                    <el-option label="全部" value=""></el-option>
                    <el-option label="审核中" value="0"></el-option>
                    <el-option label="审核通过" value="1"></el-option>
                    <el-option label="审核未通过" value="2"></el-option>
                </el-select>
            </el-col>

            <el-col :span="6">
                <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
                <el-button type="success" icon="el-icon-plus" @click="handleAdd">添加景点</el-button>
            </el-col>

<!--            <el-col :span="4">-->
<!--                <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>-->
<!--            </el-col>-->
<!--            <el-col :span="4">-->
<!--                <el-button type="success" icon="el-icon-plus" @click="handleAdd">添加景点</el-button>-->
<!--            </el-col>-->
<!--            <el-col :span="8"></el-col>-->
        </el-row>
    </div>

    <!-- 数据列表-->
    <div class="table-container">
        <el-table :data="tableData" style="width: 100%" v-loading="loading" stripe>
            <el-table-column prop="dotId" label="景点ID" width="80" align="center"></el-table-column>
            <el-table-column prop="scenicId" label="景区ID" width="80" align="center"></el-table-column>
            <el-table-column prop="dotName" label="景点名称" width="150" align="center"></el-table-column>
            <el-table-column label="景点图片" width="120" class-name="dot-image-cell">
                <template slot-scope="scope">
                    <img v-if="scope.row.dotPic" :src="scope.row.dotPic" class="dot-image" alt="景点图片">
                    <span v-else>无图片</span>
                </template>
            </el-table-column>
            <el-table-column prop="dotAddress" label="景点地址" width="200" align="center"></el-table-column>
            <el-table-column prop="dotSummary" label="景点简介" width="200" align="center"></el-table-column>

            <el-table-column prop="auditStatus" label="审核状态" width="120" align="center">
                <template slot-scope="scope">
                    <span :class="{
                        'status-pending': scope.row.auditStatus === '0',
                        'status-passed': scope.row.auditStatus === '1',
                        'status-rejected': scope.row.auditStatus === '2'
                    }">
                        {{ statusMap[scope.row.auditStatus] }}
                    </span>
                </template>
            </el-table-column>
            <!-- 当审核状态为2时，显示拒绝原因 -->
            <!-- 新增拒绝原因列 -->
            <el-table-column prop="rejectReason" label="拒绝原因" width="120" align="center">
                <template slot-scope="scope">
                    <div v-if="scope.row.auditStatus === '2'">
                        {{ scope.row.rejectReason || '无' }}
                    </div>
                    <div v-else>-</div>
                </template>
            </el-table-column>

            <el-table-column label="操作" width="150" fixed="right">
                <template slot-scope="scope">
                    <div class="action-buttons">
                        <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>

    <!-- 添加/更新表格-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="800px" :before-close="handleDialogClose">
        <el-form :model="form" :rules="rules" ref="form" label-width="120px" style="max-height: 60vh; overflow-y: auto;">
            <div class="form-section">
                <div class="form-section-title">基础信息</div>
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="景区名字" prop="scenicId">
                            <el-select v-model="form.scenicId" placeholder="请选择景区" :disabled="title==='更新信息'">
                                <el-option
                                    v-for="scenic in scenicOptions"
                                    :key="scenic.scenicId"
                                    :label="scenic.scenicName"
                                    :value="scenic.scenicId">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="景点名称" prop="dotName">
                            <el-input v-model="form.dotName" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="24">
                        <el-form-item label="景点图片" prop="dotPic">
                            <el-upload
                                    class="avatar-uploader"
                                    action="/dot/uploadImage"
                                    :show-file-list="false"
                                    :on-success="handleUploadSuccess"
                                    :before-upload="beforeUpload"
                                    :headers="uploadHeaders">
                                <img v-if="form.dotPic" :src="form.dotPic" class="avatar">
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="24">
                        <el-form-item label="景点地址" prop="dotAddress">
                            <el-input v-model="form.dotAddress" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="24">
                        <el-form-item label="景点简介" prop="dotSummary">
                            <el-input type="textarea" v-model="form.dotSummary" autocomplete="off" :rows="3"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button @click="resetForm('form')">重 置</el-button>
            <el-button type="primary" @click="submitForm('form')">确 定</el-button>
        </div>
    </el-dialog>

    <!-- 分页-->
    <div class="pagination-container">
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="[5,10,15,20,100,200]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>
    </div>
</div>

<script>
    new Vue({
        el: "#app",
        data() {
            return {
                activeIndex:"1",
                tableData: [], // 列表数据
                currentPage: 1, // 分页参数 第几页
                pageSize: 10, // 分页参数 每页显示的总条数
                total: 0, // 分页参数，列表总条数
                loading: false, // 加载状态
                searchForm: {},
                dialogFormVisible: false,
                title: '添加景点',
                form: {
                    dotId: '',
                    scenicId: '',
                    dotName: '',
                    dotPic: '',
                    dotAddress: '',
                    dotSummary: ''
                },
                statusMap: {
                    '0': '审核中',
                    '1': '审核通过',
                    '2': '审核未通过'
                },
                scenicOptions: [], // 景区下拉选项
                uploadHeaders: {
                    // 如果需要token验证，可以在这里添加
                    // 'Authorization': 'Bearer ' + localStorage.getItem('token')
                },
                formLabelWidth: '120px',
                rules: {
                    scenicId: [
                        { required: true, message: '请选择景区', trigger: 'change' },
                    ],
                    dotName: [{
                        required: true, message: '请输入景点名称', trigger: 'blur'
                    },
                        { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
                    ],
                    dotPic: [
                        { required: false, message: '请上传景点图片', trigger: 'change' }
                    ],
                    dotAddress: [
                        { required: true, message: '请输入景点地址', trigger: 'blur' }
                    ],
                    dotSummary: [
                        { required: false, message: '请输入景点简介', trigger: 'blur' }
                    ]
                }
            }
        },
        // 页面加载的时候执行
        created() {
            this.loadData();
            this.loadScenicOptions();
        },
        methods: {
            loadData() {
                this.loading = true;
                // 定义 this是vue对象
                let _this = this;
                // 向给定ID的用户发送post请求
                axios.post('/dot/queryByPage', {
                    "pageNo": this.currentPage,
                    "pageSize": this.pageSize,
                    "data": this.searchForm
                })
                    .then(function(response) {
                        // 处理成功返回
                        console.log(response);
                        // 判断返回状态
                        if (response.data.code == 200) {
                            _this.tableData = response.data.data.list;
                            _this.total = response.data.data.total;
                        }
                    })
                    .catch(function(error) {
                        // 处理错误返回
                        console.log(error);
                        _this.$message.error('数据加载失败');
                    })
                    .finally(function() {
                        // 总是会执行
                        _this.loading = false;
                    });
            },
            // 加载景区下拉选项
            loadScenicOptions() {
                let _this = this;
                axios.get('/scenic/frontQueryAll')
                    .then(function(response) {
                        if (response.data.code == 200) {
                            _this.scenicOptions = response.data.data;
                        } else {
                            _this.$message.error('景区数据加载失败: ' + response.data.msg);
                        }
                    })
                    .catch(function(error) {
                        console.log(error);
                        _this.$message.error('景区数据加载失败');
                    });
            },
            handleSizeChange(val) {
                console.log(`每页${val}条`);
                // 当前页码赋值
                this.pageSize = val;
                this.loadData();
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                // 当前页数量赋值
                this.currentPage = val;
                this.loadData();
            },
            // 搜索功能
            search() {
                this.currentPage = 1;
                this.loadData();
            },
            // 提交表单
            submitForm(f1) {
                this.$refs[f1].validate((valid) => {
                    if (valid) {
                        let uri = '/dot/add';
                        if (this.title == '更新信息') {
                            uri = '/dot/update';
                        }
                        let _this = this;
                        axios.post(uri, this.form)
                            .then(function(response) {
                                console.log(response);
                                if (response.data.code == 200) {
                                    _this.$message({
                                        message: '操作成功',
                                        type: 'success'
                                    });
                                    _this.loadData();
                                    _this.dialogFormVisible = false;
                                } else {
                                    _this.$message.error('操作失败: ' + response.data.msg)
                                }
                            })
                            .catch(function(error) {
                                // 处理错误
                                console.log(error);
                                _this.$message.error('操作出错: ' + error.message);
                            })
                            .finally(function() {
                                // 总会执行
                            });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            // 重置表单
            resetForm(f2) {
                this.$refs[f2].resetFields();
                this.form.dotPic = '';
            },
            // 处理添加,弹出添加对话框
            handleAdd() {
                this.dialogFormVisible = true;
                this.title = '添加景点';
                this.form = {
                    dotId: '',
                    scenicId: '',
                    dotName: '',
                    dotPic: '',
                    dotAddress: '',
                    dotSummary: ''
                };
            },
            // 处理更新
            handleEdit(index, row) {
                this.dialogFormVisible = true;
                this.title = '更新信息';
                this.form = JSON.parse(JSON.stringify(row));
            },
            // 处理删除
            handleDelete(index, row) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                })
                    .then(() => {
                        let _this = this;
                        axios.delete('/dot/deleteById?dotId=' + row.dotId)
                            .then(function(response) {
                                console.log(response);
                                if (response.data.code == 200) {
                                    // 提示成功
                                    _this.$message({
                                        message: '删除成功',
                                        type: 'success'
                                    });
                                    _this.loadData();
                                } else {
                                    _this.$message.error('删除失败: ' + response.data.msg)
                                }
                            })
                            .catch(function(error) {
                                // 处理错误
                                console.log(error);
                                _this.$message.error('删除出错: ' + error.message);
                            })
                            .finally(function() {});
                    }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
            },
            // 处理对话框关闭
            handleDialogClose(done) {
                this.$confirm('确认关闭？未保存的数据将会丢失')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {});
            },
            // 图片上传成功处理
            handleUploadSuccess(response, file) {
                if (response.code === 200) {
                    this.form.dotPic = response.data.url;
                    this.$message.success('图片上传成功');
                } else {
                    this.$message.error('图片上传失败: ' + response.msg);
                }
            },
            // 图片上传前验证
            beforeUpload(file) {
                const isJPGOrPNG = file.type === 'image/jpeg' || file.type === 'image/png';
                const isLt10M = file.size / 1024 / 1024 < 10;

                if (!isJPGOrPNG) {
                    this.$message.error('上传图片只能是 JPG/PNG 格式!');
                    return false;
                }
                if (!isLt10M) {
                    this.$message.error('上传图片大小不能超过 10MB!');
                    return false;
                }
                return true;
            }
        }
    });
</script>
</body>
</html>